#include "box.hpp"

Box::Box() : m_textureId(-1)
{
    //6个面 x 每个面有2个三角形组成 x 每个三角形有3个顶点
	float vertices[] = {
		-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
		 0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
		 0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
		 0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
		-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
		-0.5f, -0.5f, -0.5f,  0.0f, 0.0f,

		-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
		 0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
		 0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
		 0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
		-0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
		-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,

		-0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
		-0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
		-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
		-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
		-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
		-0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

		0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
		0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
		0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
		0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
		0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
		0.5f,  0.5f,  0.5f,  1.0f, 0.0f,

		-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
		 0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
		 0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
		 0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
		-0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
		-0.5f, -0.5f, -0.5f,  0.0f, 1.0f,

		-0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
		 0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
		 0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
		 0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
		-0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
		-0.5f,  0.5f, -0.5f,  0.0f, 1.0f
	};

	glGenVertexArrays(1, &m_VAO); //创建顶点数组对象
	glBindVertexArray(m_VAO); //绑定顶点数组

	glGenBuffers(1, &m_VBO);  //创建顶点缓冲区
	glBindBuffer(GL_ARRAY_BUFFER, m_VBO); //绑定顶点缓冲区
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); //GL_STATIC_DRAW:数据不会或几乎不会改变。

	//设置顶点属性的属性
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0); //启用0号顶点属性

	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
	glEnableVertexAttribArray(1); //启用1号顶点属性

	m_textureId = BGLTexture2D().createTextureFromImage("./data/images/girl0.jpg");
	
	m_program.createProgramFromFile("./data/shaders/11_box/vert.glsl", "./data/shaders/11_box/frag.glsl"); //创建shader program
	glEnable(GL_DEPTH_TEST);

}
Box::~Box()
{

}

void Box::Begin()
{
    m_program.begin();

	glUniform1i(glGetUniformLocation(m_program.getId(), "ourTexture"), 0);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_textureId);

    glBindVertexArray(m_VAO);
	Updata();
    glDrawArrays(GL_TRIANGLES, 0, 36);  //总共有36个顶点。
}

void Box::End()
{
    m_program.end();
}

void Box::SetCanera(BGLCamera* camera)
{
    m_camera = camera;
}

void Box::SetTexture(std::string path)
{
    if (m_textureId != -1)
    {
        glDeleteTextures(1, &m_textureId);
    }

    m_textureId = BGLTexture2D().createTextureFromImage(path);
}

void Box::SetPosition(glm::vec3 position)
{
    m_position = position;
}
void Box::SetScale(glm::vec3 scale)
{
    m_scale = scale;
}
void Box::SetRotation(glm::vec3 rotation)
{
    m_rotation = rotation;
}


void Box::Updata()
{
    glm::mat4 model = glm::mat4(1.0f);
    model = glm::rotate(model, m_rotation.x, glm::vec3(1.f, 0.0f, 0.0f));
    model = glm::rotate(model, m_rotation.y, glm::vec3(0.f, 1.0f, 0.0f));
    model = glm::rotate(model, m_rotation.z, glm::vec3(0.f, 0.0f, 1.0f));
    model = glm::scale(model,  m_scale);
    model = glm::translate(model, m_position);

    m_camera->GetMVP() * model;

    m_program.setMat4("MVP", m_camera->GetMVP() * model);
}